package view
{
	import com.greensock.TweenLite;
	
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.clearInterval;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import skin.cutphoto.Loading;
	
	public class MessagePanel extends Sprite
	{
		private  static var _instance:MessagePanel;
		private var _txt:TextField;
		private var _stage:Stage;
		private var _w:Number=450;
		private var _h:Number=40;
		private var _color:uint;
		public  static var loading:Loading=new Loading;
		private static var _mask:Sprite=new Sprite;
		public function MessagePanel($singletonEnforcer:SingletonEnforcer)
		{
			super();
			if($singletonEnforcer==null)throw new Error("singletonEnforcer");
			_txt=new TextField;
			_txt.autoSize="left";
			_txt.multiline=_txt.wordWrap=_txt.mouseEnabled=false;
			_txt.background=false;
			_txt.width=_w;
			_txt.x=_txt.y=10;
			addChild(_txt);
			setSkin();
		}
		private function setSkin():void{
			var g:Graphics=this.graphics;
			g.clear();
			g.beginFill(_color,.8);
			g.drawRect(0,0,_w,_h);
		}
		public function setRoot(s:Stage):void{
			_stage=s;
			_w=_stage.stageWidth;
			setSkin();
			_stage.addChild(this);
			this.y=-height;
			_mask=new Sprite;
			var g:Graphics=_mask.graphics;
			g.clear();
			g.beginFill(0xcccccc,.5);
		//	g.drawRect(0,0,_stage.stageWidth,_stage.stageHeight-76);
			g.drawRect(0,0,_stage.stageWidth,_stage.stageHeight);
			g.endFill();
		}
		public function showLoading(text:String,isLoading:Boolean=false,hideTime:uint=3):void
		{
			loading.txt.text=text;
			loading.txt.width=loading.txt.textWidth+4;
			loading.x=(_stage.stageWidth-loading.width)*.5;
			loading.y=(_stage.stageHeight-loading.height)*.5;
			loading.icon.visible=isLoading;
			if(!isLoading)
			{
				loading.txt.x=(300-loading.txt.width)*.5;
			}
			else
			{
				loading.txt.x=120;
			}
			_stage.addChild(_mask);
			_stage.addChild(loading);
			if(hideTime>0)
			{
				clearTimeout(timeID);
				timeID=setTimeout(callLater,hideTime*1000);
			}
		}
		private var timeID:uint;
		private function callLater():void
		{
			clearInterval(timeID);
			hideLoading();
		}
		public function hideLoading():void
		{
			if(loading.parent)
			{
				_stage.removeChild(loading);
			}
			if(_mask.parent)
			{
				_stage.removeChild(_mask);
			}
		}
		public function setText(value:String,error:Boolean=false):void{
			if(error){
				_color=0xff0000;
			}else{
				_color=0xffffcc;
			}
			setSkin();
			_txt.text=value;
			TweenLite.to(this,1,{y:0,onComplete:onCompleteH});
		}
		private var id:int=-1;
		private function onCompleteH():void{
			id=setTimeout(toMoveOut,1500);
			addEventListener(MouseEvent.MOUSE_MOVE,onMove);
		}
		private function onMove(e:MouseEvent):void{
			toMoveOut();
			removeEventListener(MouseEvent.MOUSE_MOVE,onMove);
		}
		private function toMoveOut():void{
			clearTimeout(id);
			id=-1;
			TweenLite.to(this,1,{y:-height});
		}
		
		/**
		 * 获取单例 
		 * @return 
		 * 
		 */		
		public static function getInstance():MessagePanel{
			if(MessagePanel._instance==null){
				MessagePanel._instance=new MessagePanel(new SingletonEnforcer());
			}
			return MessagePanel._instance;
		}

	}
}
class SingletonEnforcer
{	
}